package com.example.springboot.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * Swagger配置类
 */
@Configuration
public class SwaggerConfig {

    /**
     * 创建API文档配置
     */
    @Bean
    public Docket createRestApi(Environment environment) {
        // 设置环境：只有在dev、test环境下才开启Swagger
        Profiles profiles = Profiles.of("dev", "test");
        boolean enableSwagger = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .enable(enableSwagger)  // 是否启用Swagger
                .select()
                // 扫描指定包下的接口
                .apis(RequestHandlerSelectors.basePackage("com.example.springboot.controller"))
                .paths(PathSelectors.any())
                .build()
                .groupName("普通用户接口");
    }

    /**
     * API文档基本信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot整合Swagger3示例")
                .description("详细讲解SpringBoot2整合Swagger3的使用方法")
                .version("1.0.0")
                .contact(new Contact("开发者", "https://www.example.com", "dev@example.com"))
                .build();
    }
}